package com.kapcb.async.test.parallel;

import com.kapcb.async.callback.ICallback;
import com.kapcb.async.callback.IWorker;
import com.kapcb.async.executor.timer.SystemClock;
import com.kapcb.async.worker.WorkResult;
import com.kapcb.async.wrapper.WorkerWrapper;

import java.util.Map;

/**
 * <a>Title: ParTimeoutWorker </a>
 * <a>Author: kapcb <a>
 * <a>Description: ParTimeoutWorker <a>
 *
 * @author Kpacb
 * @version 1.0
 * @date 2024/5/13 12:51
 * @since 1.0
 */
public class ParallelTimeoutWorker implements IWorker<String, String>, ICallback<String, String> {

    @Override
    public void begin() {
    }

    @Override
    public void result(boolean success, String param, WorkResult<String> workResult) {
        if (success) {
            System.out.println("callback worker0 success--" + SystemClock.now() + "----" + workResult.getResult()
                    + "-threadName:" + Thread.currentThread().getName());
        } else {
            System.err.println("callback worker0 failure--" + SystemClock.now() + "----" + workResult.getResult()
                    + "-threadName:" + Thread.currentThread().getName());
        }
    }

    @Override
    public String action(String object, Map<String, WorkerWrapper<?, ?>> allWrappers) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "result = " + SystemClock.now() + "---param = " + object + " from 0";
    }

    @Override
    public String defaultValue() {
        return "worker0--default";
    }

}